package site.xuxingcan.autologin;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    //    public static Context mContext;
//    private FloatUtil floatUtil;
    private TextView money;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        mContext = this;
        money = (TextView) findViewById(R.id.money);
        SharedPreferenceUtil.getInstance();
//        floatUtil = new FloatUtil();
//        floatUtil.createFloatView(this);
        findViewById(R.id.move).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferenceUtil.getInstance().setIsMove(!SharedPreferenceUtil.getInstance().getIsMove());
            }
        });
        CheckBox mCheckBox = (CheckBox) findViewById(R.id.is_show_home);
        mCheckBox.setChecked(SharedPreferenceUtil.getInstance().getIsShowHome());
        mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                SharedPreferenceUtil.getInstance().setIsShowHome(b);
//                floatUtil.toggleHome();
                Intent mIntent = new Intent(MainActivity.this, FloatService.class);
                mIntent.setAction("toggleHome");
                startService(mIntent);
            }
        });
        findViewById(R.id.user).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, UserActivity.class));
            }
        });
        findViewById(R.id.change_dama).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new AlertDialog.Builder(MainActivity.this).setTitle("选择打码").setSingleChoiceItems(
                        new String[]{"联众", "云码"}, SharedPreferenceUtil.getInstance().getDaMa(), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                SharedPreferenceUtil.getInstance().setDaMa(i);
                                getMoney();
                            }
                        })
                        .setPositiveButton("确定", null).show();
            }
        });
        startService(new Intent(this, FloatService.class));
        getMoney();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        if (floatUtil != null) {
//            floatUtil.dismiss();
//        }
    }

    private void getMoney() {
        new Thread() {
            @Override
            public void run() {
                try {
                    final StringBuilder sb = new StringBuilder();
                    sb.append("平台:");
                    if (SharedPreferenceUtil.getInstance().getDaMa() == 0) {
                        sb.append("联众\n");
                        sb.append("账号:");
                        sb.append(SharedPreferenceUtil.getInstance().getLianZongUser());
                        sb.append("\n" + KeyUtil.Money(0));
                    } else {
                        sb.append("云码\n");
                        sb.append("账号:");
                        sb.append(SharedPreferenceUtil.getInstance().getYunmaUser());
                        sb.append("\n" + KeyUtil.Money(1));
                    }
                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                        @Override
                        public void run() {
                            money.setText(sb.toString());
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                        @Override
                        public void run() {
                            money.setText("余额获取失败");
                        }
                    });
                }
            }
        }.start();
    }
}
